package gretry

type configurableRetester interface {
	setAttempts(uint)
	setDelayCalculator(DelayCalculator)
	setRetryIf(RetryIf)
	setTimer(TimerFun)
}

type Option interface {
	apply(configurableRetester)
}

type optionFunc func(configurableRetester)

func (f optionFunc) apply(c configurableRetester) { f(c) }

// WithAttempts 实际函数被调用次数是attempts+1, 因为首次调用不被认为是尝试, 只有在之后调用出错时, 才会被开始计数,
// 因此attempts为1时, 目标函数会被调用2次; attempts为2时, 目标函数会被调用3次.
// 当attempts为0时, 目标函数将会被一直尝试直到成功.
func WithAttempts(attempts uint) Option {
	return optionFunc(func(c configurableRetester) {
		c.setAttempts(attempts)
	})
}

func WithDelayCalculator(dc DelayCalculator) Option {
	return optionFunc(func(c configurableRetester) {
		c.setDelayCalculator(dc)
	})
}

func WithRetryIf(f RetryIf) Option {
	return optionFunc(func(c configurableRetester) {
		c.setRetryIf(f)
	})
}

func WithTimerFun(f TimerFun) Option {
	return optionFunc(func(c configurableRetester) {
		c.setTimer(f)
	})
}
